home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 376-400 / disk_400 / setcpu / diskio.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  191 lines

  1. /*
  2.     SetCPU V1.60
  3.     by Dave Haynie, April 13, 1990
  4.     Released to the Public Domain
  5.  
  6.     DISKIO.C MODULE
  7.  
  8.     This module is responsible for fetching ROM images from various
  9.     kinds of disks and files.
  10. */
  11.  
  12. #include "setcpu.h"
  13.  
  14. /* This function allocates a ROM image from a KickStart disk for KICKROM. */
  15.  
  16. #define ColorShift(x)    (LONG)((((x)&0xf)+1)%15)
  17.  
  18. struct systag *AllocKSImage(name)
  19. char *name;
  20. {
  21.    struct systag *tag = NULL;
  22.    ULONG *buf = NULL, blocks;
  23.    char *ptr;
  24.    LONG unit, i, r = 0x0004L, g = 0x0008L, b = 0x000cL,start;
  25.    struct MsgPort *port = NULL;
  26.    struct IOStdReq *req = NULL;
  27.    struct Window *hand = NULL;
  28.    BOOL devok = FALSE;
  29.    
  30.  /* Then we expect it to be a kickstart disk, so we check out the device,
  31.     the disk, etc.  If all goes well, we'll be in business. */
  32.  
  33.    if ((unit = CheckTDDev(name)) == -1L) {
  34.       LoadErr = 16;
  35.       goto fail;
  36.    }
  37.    
  38.   /* This is the stuff that need opening... */
  39.    port = (struct MsgPort *) CreatePort("diskreq.port",0L);   
  40.    if (port) req = CreateStdIO(port,0L);
  41.    if (req) devok = !OpenDevice("trackdisk.device",unit,(struct IORequest *)req,0L);
  42.    if (devok) buf = (ULONG *)AllocMem(512L,MEMF_CHIP);
  43.    if (!port || !req || !devok || !buf) goto fail;
  44.  
  45.   /* Make sure we're a kick disk. */
  46.    hand = CoolHand();
  47.    while ((ReadBuf((char *)buf,0L,req) != READOK) || 
  48.           !strniequ("KICK",(char *)buf,4))
  49.       Delay(150L);
  50.       
  51.   /* Check the size of this kickstart. */
  52.  
  53.    if (!(tag = AllocMem(SizeOf(struct systag),0L))) goto fail;
  54.    start = 0;
  55.    do {
  56.       if (ReadBuf((char *)buf,++start,req) != READOK) goto fail;
  57.       ptr = (char *)SizeROM(tag,buf,TRUE);
  58.    } while (ptr == NULL && start < 32L);
  59.  
  60.    if (!ptr) goto fail;
  61.  
  62.    blocks = tag->romsize/512L;
  63.          
  64.   /* Looks like we're in shape to get the actual system. */
  65.  
  66.    SetRGB4(&(hand->WScreen->ViewPort),2L,r,g,b);
  67.    for (i = 1; i <= blocks; ++i) {
  68.       if (ReadBuf((char *)buf,i,req) != READOK) break;
  69.       MemCopy((char *)buf,ptr,512L);
  70.       ptr += 512L;
  71.       r = ColorShift(r);
  72.       g = ColorShift(g);
  73.       b = ColorShift(b);
  74.  
  75.       SetRGB4(&(hand->WScreen->ViewPort),2L,r,g,b);
  76.    }
  77.    LoadErr = 0;
  78.    
  79. done:
  80.    if (buf) FreeMem((char *)buf,512L);
  81.    if (devok) {
  82.       MotorOff(req);
  83.       CloseDevice((struct IORequest *)req);
  84.    }
  85.    if (req)  DeleteStdIO(req);
  86.    if (port) DeletePort(port);
  87.    return tag;
  88.    
  89. fail:
  90.    if (tag) {
  91.       FreeMem(tag,SizeOf(struct systag));
  92.       tag = NULL;
  93.    }
  94.    goto done;
  95. }
  96.  
  97. /* This function allocates a ROM image from an AmigaDOS file for either
  98.    KICKROM or FASTROM. */
  99.  
  100. struct systag *AllocFILEImage(name)
  101. char *name;
  102. {
  103.    struct systag *tag = NULL;
  104.    ULONG *rom = NULL, space[2], check;
  105.    BPTR file = NULL;
  106.    struct FileInfoBlock *fib;
  107.    
  108.   /* First off, let's check out this here file. */
  109.   
  110.    if (!(fib = AllocMem(SizeOf(struct FileInfoBlock),0L))) goto fail;
  111.    if (!(file = Lock(name,ACCESS_READ))) {
  112.       LoadErr = 24;
  113.       goto fail;
  114.    }
  115.    Examine(file,fib);
  116.    UnLock(file);
  117.    file = NULL;   
  118.    if (fib->fib_DirEntryType > 0L || !(file = Open(name,MODE_OLDFILE))) {
  119.       LoadErr = 24;
  120.       goto fail;
  121.    }
  122.    
  123.   /* Try to find a sensible ROM header, either its a plain ROM, or the one
  124.      with the extra sizing info at the head. */
  125.  
  126.    Read(file,(char *)space,8L);
  127.    if (space[0])
  128.       Seek(file,0L,OFFSET_BEGINNING);
  129.    else {
  130.       check = space[1];
  131.       Read(file,(char *)space,8L);
  132.       Seek(file,8L,OFFSET_BEGINNING);
  133.    }
  134.  
  135.   /* Let's allocate the space I need.  KICKROMs don't use a disk image
  136.      directly, so they don't need an MMU table and don't care about any special
  137.      alignment.  FASTROMs read from disk do care, so I will align this image. */
  138.   
  139.    SmartlyGetRange();
  140.    if (!(tag = AllocAligned(SizeOf(struct systag),8L))) goto fail;
  141.  
  142.    if (!(rom = SizeROM(tag,space,TRUE))) {
  143.       LoadErr == 22;
  144.       goto fail;
  145.    }
  146.    if (tag->romsize != Read(file,(char *)rom,tag->romsize)) {
  147.       LoadErr = 23;
  148.       goto fail;
  149.    }
  150.    Close(file);
  151.    FreeMem(fib,SizeOf(struct FileInfoBlock));
  152.    return tag;
  153.  
  154. fail:
  155.    if (file) Close(file);
  156.    if (fib) FreeMem(fib,SizeOf(struct FileInfoBlock));   
  157.    if (rom) FreeMem(rom,tag->romsize);
  158.    if (tag) FreeMem(tag,SizeOf(struct systag));
  159.    return NULL;   
  160. }
  161.  
  162. /* This function gets a ROM image from disk; in temporary unaligned memory
  163.    for a KICKROM image, permanent aligned memory for a FASTROM image. */
  164.  
  165. struct systag *AllocDISKImage(type,name)
  166. UWORD type;
  167. char *name;
  168. {
  169.    struct systag *tag;
  170.    ULONG *table;
  171.  
  172.   /* Reset the error code. */
  173.    LoadErr = 0;
  174.  
  175.   /* Let's check out this here file.  It's easy to tell; we want a FILE
  176.      image if we aren't passed a KICK_ROM request with a device name. */
  177.   
  178.    if (type == ROM_FAST || name[strlen(name)-1] != ':') {
  179.       tag = AllocFILEImage(name);
  180.       if ((tag->romtype = type) == ROM_FAST) {
  181.          FindWrap(tag);
  182.          if (!(table = AllocAligned(tag->tablesize+4,TABROUND))) return NULL;
  183.          tag->maintable = table;
  184.          FillBasicTable(tag,FALSE);
  185.       }
  186.       return tag;
  187.    } 
  188.      
  189.    return AllocKSImage(name);
  190. }
  191.